package listtest;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏
 * Date: 2024-06-09
 * Time: 20:10
 */

public class My_ArrayList implements IList {
    public int[] array;
    public int usedSize;//大小默认就是0，数组的有效长度
    public static final int DEFAULT_CAPACITY = 10;//数组的默认长度

    public My_ArrayList() {
        this.array = new int[DEFAULT_CAPACITY];
    }

    public boolean isFull() {
        return this.usedSize == this.array.length; //满了
    }

    //扩容
    private void grow() {
        //2倍扩容
        this.array = Arrays.copyOf(this.array, 2*this.array.length);
    }

    //检查pos位置下标，是否合法
    private void checkPosOf(int pos) throws IllegalPos {
        if (pos < 0 || pos > this.usedSize) {
            throw new IllegalPos("pos位置不合法");
        }
    }


    @Override
    public void add(int data) {
        if (isFull()) {
            //扩容
            grow();
        }

        this.array[this.usedSize++] = data;
    }

    @Override
    public void add(int pos, int data) {

        try {
            checkPosOf(pos);

            if (isFull()) {
                //扩容
                grow();
            }


            /**注意以下：
             * 要在某位置插入，这个位置的，前驱一定不能为空
             */
            //挪动元素
            for (int i = 0; i >= pos ; i--) {
                this.array[i+1] = this.array[i];
            }

            //插入
            this.array[pos] = data;
            usedSize++;

        }catch (IllegalPos e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            //这里如果是自己，定义的引用类型，那么要用equal比较
            if (array[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            //这里如果是自己，定义的引用类型，那么要用equal比较
            if (array[i] == toFind) {
                return i;
            }
        }
        return -1;
    }


    private void checkPosOf2(int pos) throws IllegalPos {
        if (pos < 0 || pos >= this.usedSize) {
            throw new IllegalPos("pos位置不合法");
        }
    }

    //顺序表判空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    private void checkOfList() {
        if (this.isEmpty()) {
            throw new EmptyException("顺序表为空");
        }
    }

    @Override
    public int get(int pos) {
        try {
            //顺序表本来就为空时
            this.checkOfList();
            checkPosOf2(pos);

            return array[pos];
        }catch (IllegalPos e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }

        return -1;
    }

    //更新
    @Override
    public void set(int pos, int value) {

        try {
            //顺序表本来就为空时
            this.checkOfList();
            checkPosOf2(pos);

            array[pos] = value;
        }catch (IllegalPos e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) {
        try {
            //判空
            this.checkOfList();
            //找到要删除的下标
            int pos = this.indexOf(toRemove);

            if (pos == -1) {
                System.out.println("找不到你要删除的元素");
                return;
            }

            for (int i = pos; i < usedSize-1/*这里注意,因为是array[i] = array[i+1]*/; i++) {
                this.array[i] = array[i+1];
            }
            this.usedSize--;

        }catch (EmptyException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        //如果是引用类型，要把，引用置为空
        /*for (int i = 0; i < this.usedSize; i++) {
            this.array[i] = null;
        }
        this.usedSize = 0;*/


        //基本类型直接这样写
        this.usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(array[i] +" ");
        }
    }

}
